
from langchain.chat_models import ChatOpenAI

import langchain
from langchain_core.tools import tool

from config.model_config import get_chat_openai_xin, get_chat_openai_zhipu

print(langchain.__version__)

@tool
def search(city: str) -> str:
    """获取指定城市的天气信息"""
    if "上海" in city.lower() or 'shanghai' in city.lower():
        # return '上海是中国的首都'
        return "现在30度, 有雾"
    return "现在35度, 阳光明媚"

# 定义工具结构（JSON Schema格式）
# 工具类别
tools = [search]

model = get_chat_openai_zhipu(streaming=False)

model_with_tools = model.bind_tools(tools)

# 调用工具
response = model_with_tools.invoke("上海现在天气怎么样")
# content='' additional_kwargs={'tool_calls': [{'id': 'call_6d6d001d-031c-41dc-9d1c-000bbc7f3028', 'function': {'arguments': '{"city": "上海"}', 'name': 'get_weather'},
# 'type': 'function'}], 'refusal': None} response_metadata={'token_usage': {'completion_tokens': 19, 'prompt_tokens': 166, 'total_tokens': 185, 'completion_tokens_details': None,
# 'prompt_tokens_details': None}, 'model_name': 'Qwen2.5-0.5B-Instruct', 'system_fingerprint': None, 'id': 'chatcmpl-6d6d001d-031c-41dc-9d1c-000bbc7f3028', 'service_tier': None,
# 'finish_reason': 'tool_calls', 'logprobs': None} id='run--f30260f5-f216-40de-9bc0-2f9883525679-0'
# tool_calls=[{'name': 'get_weather', 'args': {'city': '上海'}, 'id': 'call_6d6d001d-031c-41dc-9d1c-000bbc7f3028', 'type': 'tool_call'}]
# usage_metadata={'input_tokens': 166, 'output_tokens': 19, 'total_tokens': 185, 'input_token_details': {}, 'output_token_details': {}}
print(response)

print(response.tool_calls[0]['name'])
# 失败
